﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace StockEOD.Objects
{
    public class FundamentalDetail
    {
        public string Ticker { get; set; }
        public string Name { get; set; }
        public string Sector { get; set; }
        public string Industry { get; set; }
        public float PE { get; set; }
        public float EPS { get; set; }
        public float DIVYIELD { get; set; }
        public float Shares { get; set; }
        public float DPS { get; set; }
        public float PEG { get; set; }
        public float Pts { get; set; }
        public float Ptb { get; set; }
        public DateTime Date { get; set; }

        public static List<FundamentalDetail> ParseTextFile(string path)
        {
            DateTime createDate;
            string[] lines;
            if(File.Exists(path))
            {
                List<FundamentalDetail> fdList = new List<FundamentalDetail>();
                FundamentalDetail fd = null;
                lines = File.ReadAllLines(path);
                createDate = File.GetCreationTime(path);
                if (lines.Length > 1)
                {
                    for (int i = 1; i < lines.Length; i ++)
                    {
                        fd = ParseTextLine(lines[i],createDate);
                        if (fd != null)
                        {
                            fdList.Add(fd);
                        }

                    }
                    return fdList;
                }
                else
                    return null;
            }
            else
            {
                return null;
            }

        }
        public static FundamentalDetail ParseTextLine(string line, DateTime CreateDate )
        {
            FundamentalDetail fd = new FundamentalDetail();
            // Verify input

            string[] data = line.Split('\t');
            if (data.Length < 12)
            {
                throw new ArgumentException("Valid values not found in the input string");
            }
            else
            {
                fd.Ticker = data[0];
                fd.Name = data[1];
                fd.Sector = data[2];
                fd.Industry = data[3];
                fd.PE =  data[4].Length > 0 ? Convert.ToSingle(data[4]) :float.MinValue ;
                fd.EPS = data[5].Length > 0 ?  Convert.ToSingle(data[5]) : float.MinValue;
                fd.DIVYIELD = data[6].Length > 0 ? Convert.ToSingle(data[6]) : float.MinValue;
                fd.Shares = data[7].Length > 0 ? Convert.ToSingle(data[7]) : float.MinValue;
                fd.DPS = data[8].Length > 0 ?  Convert.ToSingle(data[8]) : float.MinValue;
                fd.PEG = data[9].Length > 0 ?  Convert.ToSingle(data[9]) : float.MinValue;
                fd.Pts = data[10].Length > 0 ?  Convert.ToSingle(data[10]) : float.MinValue;
                fd.Ptb = data[11].Length > 0 ?  Convert.ToSingle(data[11]) :float.MinValue;
                fd.Date = CreateDate;

            }
            return fd;
            

        }
    }
}
